package wserver

import (
	"config"
	"fmt"
	"github.com/go-martini/martini"
	"github.com/martini-contrib/auth"
	"github.com/martini-contrib/binding"
	"github.com/martini-contrib/render"
	"wserver/routes"
)

func StartWebServer() {
	m := martini.Classic()
	m.Use(martini.Static(config.PublicDirectory))
	m.Use(render.Renderer(render.Options{
		Directory:  config.TemplateDirectory,   // Specify what path to load the templates from.
		Layout:     "layout",                   // Specify a layout template. Layouts can call {{ yield }} to render the current template.
		Extensions: []string{".tmpl", ".html"}, // Specify extensions to load for templates.
		//Funcs:           []template.FuncMap{AppHelpers}, // Specify helper function maps for templates to access.
		Delims:          render.Delims{"{{", "}}"}, // Sets delimiters to the specified strings.
		Charset:         "UTF-8",                   // Sets encoding for json and html content-types. Default is "UTF-8".
		IndentJSON:      true,                      // Output human readable JSON
		IndentXML:       true,                      // Output human readable XML
		HTMLContentType: "text/html",               // Output XHTML content type instead of default "text/html"
	}))

	m.Use(auth.BasicFunc(func(username, password string) bool {
		return username == "admin" && password == "guessme"
	}))

	m.Get("/", routes.Index)
	m.Get("/confirmDlg", routes.ConfirmDlg)

	m.Get("/getAccounts", routes.GetAccMgr)
	m.Get("/editAccount", routes.EditAcc)
	m.Get("/getAccountInfo", routes.GetAccountInfo)
	m.Get("/checkAccountName", routes.CheckAccountExist)
	m.Post("/_saveAccount", binding.Bind(routes.AccountEditValue{}), routes.SaveAccount)
	m.Post("/_delAccount", routes.DeleteAccount)

	// TEST Entry
	m.Get("/getTestEntryView", routes.GetTestEntryView)
	m.Get("/getTestSendMsg2AccountView", routes.GetTestSendMsg2AccountView)
	m.Get("/checkMsgAccountTo", routes.CheckMsgAccountTo)
	m.Post("/_sm2a", binding.Bind(routes.Message2AccountEditValue{}), routes.SendMessage2Account)

	// farm
	m.Get("/getFarms", routes.GetFarms)
	m.Get("/getFarmInfo", routes.GetFarmInfo)
	m.Get("/editFarm", routes.EditFarm)
	m.Post("/_saveFarm", binding.Bind(routes.FarmEditValue{}), routes.SaveFarm)
	m.Get("/addFarmController", routes.AddFarmController)
	m.Get("/checkFarmController", routes.CheckFarmController)
	m.Post("/_delFarmControllers", routes.DelFarmControllers)
	m.Post("/_saveFarmController", binding.Bind(routes.FarmControllerEditValue{}), routes.SaveFarmController)

	// controller
	m.Get("/getControllers", routes.GetControllerMgr)
	m.Get("/getControllerInfo", routes.GetControllerInfo)

	// task
	m.Get("/getTasks", routes.GetTasks)
	m.Get("/editTask", routes.EditTask)
	m.Post("/_saveTask", binding.Bind(routes.TaskEditValue{}), routes.SaveTask)
	m.Get("/getTaskInfo", routes.GetTaskInfo)
	m.Get("/addTaskController", routes.AddTaskController)
	m.Get("/checkTaskController", routes.CheckTaskController)
	m.Post("/_saveTaskController", binding.Bind(routes.TaskControllerEditValue{}), routes.SaveTaskController)
	m.Post("/_delTaskControllers", routes.DelTaskControllers)

	m.RunOnAddr(fmt.Sprintf(":%d", config.WebPort))
}
